package com.wkhyapp.lm.view;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.lidong.photopicker.intent.PhotoPreviewIntent;
import com.lidong.photopicker.intent.PhotoPreviewIntent2;
import com.umeng.commonsdk.debug.E;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.GoodsListAdapter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.constant.Constant;
import com.wkhyapp.lm.contract.ShopPresenter;
import com.wkhyapp.lm.contract.ShopView;
import com.wkhyapp.lm.fragment.ShopGoodsFragment;
import com.wkhyapp.lm.fragment.ShopGoodsImageFragment;
import com.wkhyapp.lm.http.vo.Category;
import com.wkhyapp.lm.http.vo.Follows;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.http.vo.Member;
import com.wkhyapp.lm.http.vo.Notice;
import com.wkhyapp.lm.utils.DensityUtil;
import com.wkhyapp.lm.utils.ImageLoadUtil;
import com.wkhyapp.lm.utils.ImgDonwloads;
import com.wkhyapp.lm.utils.MemberUtils;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.utils.StringUtils;
import com.wkhyapp.lm.weigit.EmptyView;
import com.wkhyapp.lm.weigit.dialog.CategoryDialog;
import com.wkhyapp.lm.weigit.dialog.ContactShopDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShopActivity extends SuperActivity<ShopPresenter> implements ShopView{

    private Integer userId;
    ImageView  head_iv;
    TextView  name_tv;
    TextView remk_tv;
    TextView shGoodsNameTv;
    TextView shGoodsLineTv;
    TextView shGoodsImgTv;
    TextView shGoodsImgLineTv;
    CategoryDialog categoryDialog;
    @BindView(R.id.all_rl)
    RelativeLayout all_rl;
    private int typeId = 0;
    @BindView(R.id.back_iv)
    ImageView back;
    TextView goods_tv,views_tv;
   // RelativeLayout gz_rl;
    @BindView(R.id.lxkf_rl)
    RelativeLayout lxkf_rl;
    private List<Category> categories = new ArrayList<>();
    private Member member;
    private ImageView lv_iv;
    @BindView(R.id.sousuo_rl)
    RelativeLayout sousuo_rl;
    View empty_view;
    @BindView(R.id.tousu_iv)
    ImageView tousu_iv;
    Button gz_but;
    private Fragment framgentContent;
    private ShopGoodsFragment shopGoodsFragment;
    private ShopGoodsImageFragment shopGoodsImageFragment;
    @Override
    protected ShopPresenter createPresenter() {
        return mPresenter = new ShopPresenter(this);
    }


    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);
        userId = getIntent().getIntExtra("0",0);
        if (shopGoodsFragment == null) {
            shopGoodsFragment = new ShopGoodsFragment();
        }

        if (shopGoodsImageFragment == null) {
            shopGoodsImageFragment = new ShopGoodsImageFragment();
        }
//        goodses = new ArrayList<>();
        empty_view = EmptyView.getView(mContext,R.layout.empty_view,R.drawable.wuzhuangtai,"暂无上架商品");
//        headView = View.inflate(mContext, R.layout.shop_head, null);
        head_iv = (ImageView) this.findViewById(R.id.head_iv);
        name_tv = (TextView) this.findViewById(R.id.name_tv);
        remk_tv = (TextView) this.findViewById(R.id.remk_tv);
        goods_tv = (TextView) this.findViewById(R.id.goods_tv);
        views_tv = (TextView) this.findViewById(R.id.views_tv);

         shGoodsNameTv  = (TextView) this.findViewById(R.id.shGoodsNameTv);
         shGoodsLineTv = (TextView) this.findViewById(R.id.shGoodsLineTv);
         shGoodsImgTv = (TextView) this.findViewById(R.id.shGoodsImgTv);
         shGoodsImgLineTv = (TextView) this.findViewById(R.id.shGoodsImgLineTv);
//        gz_rl = (RelativeLayout) headView.findViewById(R.id.gz_rl);
//        gz_tv = (TextView) headView.findViewById(R.id.gz_tv);
        lv_iv = (ImageView)this.findViewById(R.id.lv_iv2);
         gz_but = (Button) this.findViewById(R.id.gz_but);
        mPresenter.getData(userId,0);
        mPresenter.getCategry(userId);
        mPresenter.getShop(userId);
        showLoad("正在加载数据...");
        shopGoodsFragment.setUserId(userId);
        shopGoodsImageFragment.setUserId(userId);
//        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_shop_container, shopGoodsImageFragment).commit();
//        framgentContent = shopGoodsImageFragment;
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_shop_container, shopGoodsFragment).commit();
        framgentContent = shopGoodsFragment;
    }

    private void replaceFragment(Fragment fragment) {
        if (!fragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().hide(framgentContent).add(R.id.fragment_shop_container, fragment).commit();
        } else {
            getSupportFragmentManager().beginTransaction().hide(framgentContent).show(fragment).commit();
        }
        framgentContent = fragment;
    }

    @Override
    protected void initListener() {

        tousu_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(FeedbackActivity.class);
            }
        });


        shGoodsNameTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shGoodsLineTv.setVisibility(View.VISIBLE);
                shGoodsImgLineTv.setVisibility(View.INVISIBLE);
                replaceFragment(shopGoodsFragment);
            }
        });
        shGoodsImgTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shGoodsImgLineTv.setVisibility(View.VISIBLE);
                shGoodsLineTv.setVisibility(View.INVISIBLE);
                replaceFragment(shopGoodsImageFragment);
            }
        });


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        all_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(categories.size()>0){
                    categoryDialog.show();
                }else{
                    TToast("商家暂无分类");
                }
            }
        });

        sousuo_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goTo(SearchActivity.class);
            }
        });


        gz_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!MemberUtils.isLogin()){
                    TToast("请先登录...");
                    goTo(LoginActivity.class);
                }else{
                    if(member!=null){
                        mPresenter.follow(member.getId());
                    }
                }

            }
        });


        lxkf_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final   ContactShopDialog contactShopDialog = new ContactShopDialog(mContext,member.getWechatAccount(),member.getMobile());
                contactShopDialog.show();
                contactShopDialog.setCallBack(new ContactShopDialog.callBack() {
                    @Override
                    public void weixin(String str) {

                        if(!StringUtils.isEmpty(str)){
                            mPresenter.ccopy(userId);
                            TToastLong("微信号已复制，请添加联系商家");
                            ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                            ClipData clipData = ClipData.newPlainText(null, str);
                            // 把数据集设置（复制）到剪贴板
                            clipboard.setPrimaryClip(clipData);

                            try{
                                Intent lan = mContext.getPackageManager().getLaunchIntentForPackage("com.tencent.mm");
                                Intent intent = new Intent(Intent.ACTION_MAIN);
                                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setComponent(lan.getComponent());
                                startActivity(intent);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                        contactShopDialog.close();

                    }

                    @Override
                    public void phone(String str) {
                        if(StringUtils.isEmpty(str)){
                            TToast("商家暂无未设置联系手机");
                            return;
                        }
                        try {
                            Intent intent = new Intent(Intent.ACTION_DIAL);
                            Uri data = Uri.parse("tel:" + str);
                            intent.setData(data);
                            startActivity(intent);
                        }catch (Exception e){
                            e.printStackTrace();
                            TToast("请手动拨打电话");
                        }
                    }
                });
            }
        });
    }


    @Override
    public void setGoods(List<Goods> list) {


    }

    @Override
    public void setGoodsMore(List<Goods> list) {

    }




    @Override
    public void setCategoory(List<Category> list) {
        Category category = new Category();
        category.setName("全部");
        category.setId(0);
        list.add(0,category);
        categories.addAll(list);
        categoryDialog = new CategoryDialog(mContext,categories);
        categoryDialog.init();
        categoryDialog.setCallBack(new CategoryDialog.callBack() {
            @Override
            public void jump(Category item) {
                typeId = item.getId();
                showLoad("正在加载数据...");
                mPresenter.getData(userId,item.getId());
                shopGoodsFragment.refleshGoodsByTyp(typeId);
            }
        });
    }

    @Override
    public void setShop(Member member) {
        this.member = member;
        if(member.getHeadImg().startsWith("http")){
            ImageLoadUtil.setCirlcleImage_Normals(mContext,member.getHeadImg(),  head_iv);
//            ImageLoadUtil.setRoundImage_Normal(mContext,member.getHeadImg(), 8, head_iv, 1);
        }else{
//            ImageLoadUtil.setRoundImage_Normal(mContext, Constant.DEF_HEAD_2+member.getHeadImg(),DensityUtil.dip2px(mContext, 8),head_iv);
            ImageLoadUtil.setCirlcleImage_Normals(mContext, Constant.DEF_HEAD_2+member.getHeadImg(), head_iv);

        }
        https://a.wkhyapp.com/
        name_tv.setText(member.getNickname());
        remk_tv.setText(member.getRemark());
        goods_tv.setText(member.getGoods()+"");
        views_tv.setText(member.getViews()+"");

        if("2".equals(member.getLevelId())){
            ImageLoadUtil.setImage_Normal(mContext,R.drawable.hj,lv_iv);
        }else if("3".equals(member.getLevelId())){
            ImageLoadUtil.setImage_Normal(mContext,R.drawable.zs,lv_iv);
        }else if("4".equals(member.getLevelId())){
            ImageLoadUtil.setImage_Normal(mContext,R.drawable.zz,lv_iv);
        }else if("5".equals(member.getLevelId())){
            lv_iv.setVisibility(View.VISIBLE);
            ImageLoadUtil.setImage_Normal(mContext,R.drawable.v1,lv_iv);
        }else if("6".equals(member.getLevelId())){
            lv_iv.setVisibility(View.VISIBLE);
            ImageLoadUtil.setImage_Normal(mContext,R.drawable.v2,lv_iv);
        }else if("7".equals(member.getLevelId())){
            lv_iv.setVisibility(View.VISIBLE);
            ImageLoadUtil.setImage_Normal(mContext,R.drawable.v3,lv_iv);
        }else{
            lv_iv.setVisibility(View.GONE);
        }

        if(member.isFollow()){
            gz_but.setText("已关注");
            gz_but.setTextColor(Color.parseColor("#A3A3A3"));
            gz_but.setBackgroundResource(R.drawable.shape_guanzhu_un_share);
        }else{
            gz_but.setText("关注");
            gz_but.setTextColor(Color.parseColor("#333333"));
            gz_but.setBackgroundResource(R.drawable.shape_product_share);
        }


    }

    @Override
    public void setFollows(Follows str) {
        if(str.isFlag()){
            gz_but.setText("已关注");
            gz_but.setTextColor(Color.parseColor("#A3A3A3"));
            gz_but.setBackgroundResource(R.drawable.shape_guanzhu_un_share);
        }else{
            gz_but.setText("关注");
            gz_but.setTextColor(Color.parseColor("#333333"));
            gz_but.setBackgroundResource(R.drawable.shape_product_share);
        }

        TToast(str.getMsg());
        mPresenter.getShop(member.getId());
    }


    @Override
    public void succ() {

    }

    @Override
    public void checkVersion() {
        lxkf_rl.setVisibility(View.VISIBLE);
        remk_tv.setVisibility(View.VISIBLE);
    }


    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop;
    }


}
